package com.syas.service.services;

import com.syas.base.service.services.ChainEntityService;
import com.syas.common.exception.EntityOperateException;
import com.syas.common.exception.ValidatException;
import com.syas.common.extension.StringHelper;
import com.syas.common.utilities.PageList;
import com.syas.common.utilities.PageListUtil;
import com.syas.dao.interfaces.IFmgerDao;
import com.syas.model.models.Fmger;
import com.syas.service.interfaces.IFmgerService;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("FmgerService")
public class FmgerService extends ChainEntityService<Integer, Fmger, IFmgerDao> implements IFmgerService {

    @Autowired
    public FmgerService(@Qualifier("FmgerDao") IFmgerDao fmgerDao){
        super(fmgerDao);
    }
    @Override
    @SuppressWarnings("unchecked")
    public PageList<Fmger> listPage(String name,
                                    String ezubouname,
                                    String mobileno,
                                    String certid,
                                    int pageNo,
                                    int pageSize) {
		Criteria countCriteria = entityDao.getCriteria();
		Criteria listCriteria = entityDao.getCriteria();
		if(!StringHelper.isEmpty(name)){
			countCriteria.add(Restrictions.eq("realName", name));
    		listCriteria.add(Restrictions.eq("realName", name));
		}
        if(!StringHelper.isEmpty(ezubouname)){
			countCriteria.add(Restrictions.eq("ezboUname", ezubouname));
    		listCriteria.add(Restrictions.eq("ezboUname", ezubouname));
		}
        if(!StringHelper.isEmpty(mobileno)){
            countCriteria.add(Restrictions.eq("mobileNo", mobileno));
            listCriteria.add(Restrictions.eq("mobileNo", mobileno));
        }
        if(!StringHelper.isEmpty(certid)){
            countCriteria.add(Restrictions.eq("certId", certid));
            listCriteria.add(Restrictions.eq("certId", certid));
        }
        listCriteria.setFirstResult((pageNo-1)*pageSize);
        listCriteria.setMaxResults(pageSize);
        List<Fmger> items = listCriteria.list();
        countCriteria.setProjection(Projections.rowCount());
        Integer count=StringHelper.convertStringToInt(countCriteria.uniqueResult());
       /*  StringBuffer sb = new StringBuffer();
        sb.append("from Fmger f,Organization o where f.orgId = o.id ");
        sb.append("from Fmger f where 1 = 1 ");
        if(!StringHelper.isEmpty(name)){
            sb.append(" and f.realName = '" + name + "'");
        }
        if(!StringHelper.isEmpty(ezubouname)){
            sb.append(" and f.ezboUname = '" + ezubouname + "'");
        }
        if(!StringHelper.isEmpty(mobileno)){
            sb.append(" and f.mobileNo = '" + mobileno + "'");
        }
        if(!StringHelper.isEmpty(certid)){
            sb.append(" and f.certId = '" + certid +"'");
        }
        List<Fmger> items = entityDao.getFmgerList(sb.toString());
        Integer count = 0;
        if(items != null)
            count = items.size();*/
        return PageListUtil.getPageList(count, pageNo, items, pageSize);
    }
    public void delete() throws EntityOperateException, ValidatException {
        entityDao.delete();
    }
}
